﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using XchangeStreamer.Publisher.SDK;
using XchangeStreamer.Types;
using System.Threading;

namespace XchangeStreamer.Publisher
{
    class Program
    {
        private static Connector connector;
        private static long stop = 0;
        
        static void Main(string[] args)
        {
            Console.Write("Please enter streamer info (IP/Port):");
            string[] streamerInfo = Console.ReadLine().Split('/');

            Console.Write("Please enter cache server info (IP/Port):");
            string[] cacheServerInfo = Console.ReadLine().Split('/');

            string streamerIP = streamerInfo.Length < 2 ? "localhost" : streamerInfo[0];
            string streamerPort = streamerInfo.Length < 2 ? null : streamerInfo[1];
            string cacheServerIP = cacheServerInfo.Length < 2 ? "localhost" : cacheServerInfo[0];
            string cacheServerPort = cacheServerInfo.Length < 2 ? null : cacheServerInfo[1];

            connector = Connector.Connection;
            connector.Connect(streamerIP, streamerPort, cacheServerIP, cacheServerPort);

            connector.CreateTopic("Orders");
            connector.CreateTopic("Trades");

            var start = DateTime.UtcNow.TimeOfDay;
            Console.WriteLine("Start Time: {0}", start);

            Task.Factory.StartNew(PublishOrders);
            Task.Factory.StartNew(PublishTrades);

            Console.ReadKey();
            Interlocked.Increment(ref stop);
            connector.Disconnect();

            var end = DateTime.UtcNow.TimeOfDay;
            Console.WriteLine("End Time: {0}", end);

            Console.WriteLine("Test Time: {0}", end.Subtract(start));
            Console.ReadKey();
        }

        private static void PublishOrders()
        {
            int count = 500;
            var orders = new List<Order>(count);
            for (int i = 0; i < count; i++)
            {
                Order order = new Order();
                order.Date = DateTime.MaxValue;
                order.Time = TimeSpan.MaxValue;
                order.Exchange = "EGXEGXEGXEGX";
                order.LeValue = decimal.MaxValue;
                order.MarketCode = "PMPMPMPMPMPM";
                order.OrderAction = QuoteStatus.Open;
                order.OrderNumber = "123456789012";
                order.OrderType = QuoteType.Buy;
                order.Price = decimal.MaxValue;
                order.RecSerial = int.MaxValue;
                order.SymbolCode = "ORTE.CAORTE.";
                order.Volume = uint.MaxValue;
                order.Value = decimal.MaxValue;

                orders.Add(order);
            }

            Random random = new Random();
            var c = true;
            while (c)
            {
                //c = false;
                int index = random.Next(1, count);
                int size = connector.Publish<Order, List<Order>>(1, orders.GetRange(0, index));
                //Console.WriteLine("Type: {0} | Count: {1} | Size: {2}", "Order", index, size);
                //System.Threading.Thread.Sleep(500);
                if (Interlocked.Read(ref stop) == 1)
                {
                    c = false;
                }
            }
        }

        private static void PublishTrades()
        {
            int count = 500;
            var trades = new List<Trade>(count);
            for (int i = 0; i < count; i++)
            {
                Trade trade = new Trade();
                trade.BuyOrderNumber = "123456789012";
                trade.Date = DateTime.MaxValue;
                trade.LeValue = decimal.MaxValue;
                trade.MarketCode = "PMPMPMPMPMPM";
                trade.Price = decimal.MaxValue;
                trade.SellOrderNumber = "123456789012";
                trade.SymbolCode = "ORTE.CAORTE.";
                trade.TicketNumber = "123456789012";
                trade.Time = TimeSpan.MaxValue;
                trade.Volume = uint.MaxValue;
                trade.Value = decimal.MaxValue;

                trades.Add(trade);
            }

            Random random = new Random();
            var c = true;
            while (c)
            {
                //c = false;
                int index = random.Next(1, count);
                int size = connector.Publish<Trade, List<Trade>>(2, trades.GetRange(0, index));
                //Console.WriteLine("Type: {0} | Count: {1} | Size: {2}", "Trade", index, size);
                //System.Threading.Thread.Sleep(500);
                if (Interlocked.Read(ref stop) == 1)
                {
                    c = false;
                }
            }
        }
    }
}
